<?php
/**
 * Attach components to a project
 *
 */

class Attach extends Controller {

	function Attach() {
		parent::Controller();
	}

	/**
	 * Attach a department to a project
	 *
	 * @param	integer	$id	the project id
	 * @param	integer	by POST	the department id
	 */
	function department($id) {
		assert(is_numeric($id));
	
		$this->project_id = $id;
		if( isset($_POST['submit']) ) {
			$department_id = $this->input->post('department_id');
			$accounts = $this->Accounts->fetch_by_department($department_id);
			foreach( $accounts as $account ) {
				$this->Projects->attach_account($id, $account->account_id);
			}
			redirect( 'project/view/id/'.$id.'#attached-accounts', 'location');
		}
		$this->load->view('project/attach/department');
	}

	/**
	 * Attach a comment to a project
	 *
	 * @param	integer	$id	the project id
	 * @param	string	by POST the comment text
	 */
	function comment($id) {
		assert(is_numeric($id));

		$this->project_id = $id;
		if( isset($_POST['submit']) ) {
			$comment = trim($this->input->post('comment'));
			if(empty($comment)) {
				$this->error = 'Please enter a text';
			} else {
				$this->ProjectComments->create($id, $comment);
				redirect( 'project/view/id/'.$id.'#comments', 'location');
			}
		}
		$this->load->view('project/attach/comment');
	}

}

?>
